package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventBfPlan;
import com.ruoyi.event.domain.bo.EventBfPlanBo;
import com.ruoyi.event.domain.vo.EventBfPlanVo;
import com.ruoyi.event.mapper.EventBfPlanMapper;
import com.ruoyi.event.service.IEventBfPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 帮扶计划Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventBfPlanServiceImpl implements IEventBfPlanService {

    private final EventBfPlanMapper baseMapper;

    /**
     * 查询帮扶计划
     */
    @Override
    public EventBfPlanVo queryById(Long bfPlanId) {
        return baseMapper.selectVoById(bfPlanId);
    }

    /**
     * 查询帮扶计划列表
     */
    @Override
    public TableDataInfo<EventBfPlanVo> queryPageList(EventBfPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventBfPlan> lqw = buildQueryWrapper(bo);
        Page<EventBfPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询帮扶计划列表
     */
    @Override
    public List<EventBfPlanVo> queryList(EventBfPlanBo bo) {
        LambdaQueryWrapper<EventBfPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventBfPlan> buildQueryWrapper(EventBfPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventBfPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventBfPlan::getUserId, bo.getUserId());
        lqw.eq(bo.getMemberId() != null, EventBfPlan::getMemberId, bo.getMemberId());
        lqw.eq(bo.getZxsj() != null, EventBfPlan::getZxsj, bo.getZxsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJhdd()), EventBfPlan::getJhdd, bo.getJhdd());
        lqw.eq(StringUtils.isNotBlank(bo.getJhzy()), EventBfPlan::getJhzy, bo.getJhzy());
        lqw.eq(StringUtils.isNotBlank(bo.getJhnr()), EventBfPlan::getJhnr, bo.getJhnr());
        return lqw;
    }

    /**
     * 新增帮扶计划
     */
    @Override
    public Boolean insertByBo(EventBfPlanBo bo) {
        EventBfPlan add = BeanUtil.toBean(bo, EventBfPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBfPlanId(add.getBfPlanId());
        }
        return flag;
    }

    /**
     * 修改帮扶计划
     */
    @Override
    public Boolean updateByBo(EventBfPlanBo bo) {
        EventBfPlan update = BeanUtil.toBean(bo, EventBfPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventBfPlan entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除帮扶计划
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
